.section .data
    num: .int 0x0812abcd
.section .text
.globl _start
_start:
    movl    num, %eax
    pushl   %eax
    call add_sign

.type add_sign, @function
add_sign:
    pushl   %ebp
    movl    %esp, %ebp
    subl    $16, %esp
    movl    %ebx, 12(%esp)
    movl    8(%ebp), %eax
    leal    10(%esp), %ecx # %ecx is s[i]
    leal    3(%esp), %edx
    testl   %eax, %eax
    jnz L1
    movb    $'0', (%ecx)
    decl    %ecx
    jmp L2
L1:
    jns L3
    negl    %eax
L3:
    testl   %eax, %eax
    jz L2
    movb    %al, %dl 
    andb    $0xf, %dl
    cmpb    $10, %dl
    jb L4
    addb    $87, %dl       # %dl + 87 is the ascii code of lower case character
    jmp L5
L4:
    addb    $'0', %dl
L5:
    movb    %dl, (%ecx)
    decl    %ecx
    shrl    $4, %eax
    cmpl    %ebx, %ecx
    jae L3
L2:                         # add prefix 0x
    movb    $'x', (%ecx)
    decl    %ecx
    movb    $'0', (%ecx)
    movl    8(%ebp), %eax
    testl   %eax, %eax
    jns L6
    decl    %ecx
    movb    $'-', (%ecx)    # if < 0, add '-'
L6:
    movl    $4, %eax
    movl    $1, %ebx
    leal    12(%esp), %edx
    subl    %ecx, %edx
    int $0x80               # print the output in the terminal
    movl    12(%esp), %ebx
    popl    %ebp
    ret




